package edu.fudan.jwxt.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.fudan.jwxt.Exception.ServiceException;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.dao.CollegeDao;
import edu.fudan.jwxt.dao.MajorDao;
import edu.fudan.jwxt.dto.CollegeDto;
import edu.fudan.jwxt.entity.College;
import edu.fudan.jwxt.entity.Major;
import edu.fudan.jwxt.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeDao, College> implements CollegeService {
    @Autowired
    private CollegeDao collegeDao;

    @Autowired
    private MajorDao majorDao;

    public List<CollegeDto> listAll() {
        List<College> colleges = list();
        List<CollegeDto> collegeDtos = new ArrayList<>();
        for (College college : colleges){
            CollegeDto collegeDto = new CollegeDto();
            LambdaQueryWrapper<Major> q = new LambdaQueryWrapper<>();
            q.eq(Major::getCollegeId,college.getId());
            List<Major> majors = majorDao.selectList(q);

            BeanUtil.copyProperties(college,collegeDto,true);
            collegeDto.setMajors(majors);
            collegeDtos.add(collegeDto);
        }
        return collegeDtos;
    }

    @Override
    public boolean save(College college){
        LambdaQueryWrapper<College> q = new LambdaQueryWrapper<>();
        q.eq(College::getName,college.getName());
        College flag = collegeDao.selectOne(q);
        if(flag != null){
            throw new ServiceException(Constants.CODE_600,"学院名称已存在");
        }

        //格式检查

        collegeDao.insert(college);
        return true;
    }

    @Override
    public boolean updateById(College college) {
        LambdaQueryWrapper<College> q = new LambdaQueryWrapper<>();
        q.eq(College::getName, college.getName()).ne(College::getId,college.getId());
        College flag = collegeDao.selectOne(q);
        if (flag != null) {
            throw new ServiceException(Constants.CODE_600, "学院名称已存在");
        }

        //格式检查

        int count = collegeDao.updateById(college);
        return count != 0;
    }

    @Override
    public boolean removeById(Serializable id) {
        LambdaQueryWrapper<Major> q1 = new LambdaQueryWrapper<>();
        q1.eq(Major::getCollegeId,id);
        long majorCount = majorDao.selectCount(q1);

        if(majorCount != 0)
            throw new ServiceException(Constants.CODE_600,"该学院存在专业，无法删除");

        return super.removeById(id);
    }

    @Override
    public College getById(Serializable id){
        College college = collegeDao.selectById(id);
        if(college == null)
            throw new ServiceException(Constants.CODE_600,"学院不存在");
        return college;
    }

    @Override
    public String getNameById(Serializable id){
        College college=getById(id);
        return college.getName();
    }
}
